const trimStart = (
  arr: Array<number | string | boolean>,
  value: number | string | boolean | ((item: number | string | boolean, index?: number | string) => boolean),
): Array<number | string | boolean> => {
  if (typeof value === 'function') {
    if (value(arr[0])) {
      arr.shift();
      return trimStart(arr, value);
    } else {
      return arr;
    }
  } else {
    if (arr[0] === value) {
      arr.shift();
      return trimStart(arr, value);
    } else {
      return arr;
    }
  }
};

const trimEnd = (
  arr: Array<number | string | boolean>,
  value: number | string | boolean | ((item: number | string | boolean, index?: number | string) => boolean),
): Array<number | string | boolean> => {
  if (typeof value === 'function') {
    if (value(arr[arr.length - 1])) {
      arr.pop();
      return trimEnd(arr, value);
    } else {
      return arr;
    }
  } else {
    if (arr[arr.length - 1] === value) {
      arr.pop();
      return trimEnd(arr, value);
    } else {
      return arr;
    }
  }
};

const trim = (
  arr: Array<number | string | boolean>,
  value: number | string | boolean | ((item: number | string | boolean, index?: number | string) => boolean),
) => {
  return trimEnd(trimStart(arr, value), value);
};

export { trim, trimStart, trimEnd };
